package com.letao.server.controller;


import com.letao.server.dto.Result;
import com.letao.server.entity.ShopInfo;
import com.letao.server.service.IShopService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 商店模块控制类
 */
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private IShopService shopService;

    /**
     * 增加商铺信息
     * 接口等级: 管理员
     *
     * @param shopInfo 店铺信息
     * @return 返回的结果
     */
    @PostMapping
    private Result insert(@RequestBody ShopInfo shopInfo) {
        // 1.检测内容是否完整
        if (shopInfo.getDescript() == null || shopInfo.getDetail() == null || shopInfo.getName() == null
                || shopInfo.getCityId() == null || shopInfo.getProvinceId() == null || shopInfo.getDistrictId() == null) {
            return Result.fail("数据不完整");
        }
        if (shopInfo.getLatitude() == null && shopInfo.getLongitude() == null) {
            shopInfo.setLatitude(31.880583);
            shopInfo.setLongitude(118.819617);
        }
        // 2.增加商铺信息
        shopService.save(shopInfo);
        log.info("管理员新增了Id={}的店铺数据", shopInfo.getId());
        return Result.success();
    }

    /**
     * 删除商铺信息
     * 接口等级: 管理员
     *
     * @param id 要删除的商铺的Id
     * @return 返回的结果
     */
    @DeleteMapping("/{id}")
    private Result delete(@PathVariable("id") Integer id) {
        boolean b = shopService.removeById(id);

        if (!b) {
            return Result.fail("商铺不存在");
        }
        log.info("管理员删除了Id={}的店铺数据", id);
        return Result.success();
    }

    /**
     * 修改商铺信息
     * 接口等级: 管理员
     *
     * @param shopInfo 修改后的商铺信息
     * @return 返回的结果
     */
    @PutMapping
    private Result update(@RequestBody ShopInfo shopInfo) {
        if (shopInfo.getId() == null) {
            return Result.fail("数据不完整");
        }
        shopInfo.setCreateTime(null);
        shopInfo.setModifiedTime(null);
        boolean b = shopService.updateById(shopInfo);
        if (!b) {
            return Result.fail("店铺不存在");
        }
        log.info("管理员修改了Id={}的店铺信息", shopInfo.getId());
        return Result.success();
    }

    /**
     * 分页查询店铺信息
     * 接口等级:管理员
     *
     * @param name       店铺名
     * @param provinceId 省份Id
     * @param cityId     城市Id
     * @param districtId 区Id
     * @param page       页码
     * @param pageSize   页大小
     * @return 返回的结果
     */
    @GetMapping
    private Result queryShops(
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer provinceId,
            @RequestParam(required = false) Integer cityId,
            @RequestParam(required = false) Integer districtId,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        return shopService.getPageInfo(id, name, provinceId, cityId, districtId, page, pageSize);
    }

    /**
     * 查询推荐地图
     * 接口等级:用户
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return 返回的结果
     */
    @GetMapping("/suggect")
    private Result suggectShops(@RequestParam Double longitude,
                                @RequestParam Double latitude) {
        return shopService.suggect(longitude, latitude);
    }

}
